package com.leetcode.LC_MED;

import java.util.*;

public class LC347 {
    //https://www.jb51.net/article/90537.htm
    public int[] topKFrequent(int[] nums, int k) {

        Map<Integer, Integer> map = new HashMap<>();
        for (int num : nums) {
            if (map.containsKey(num)) {
                map.put(num, map.get(num) + 1);
            } else {
                map.put(num, 1);
            }
        }
        ArrayList<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet());
        list.sort((o1, o2) -> o2.getValue() - o1.getValue());

        int[] ints = new int[k];
        for (int i = 0; i < k; i++) {
            ints[i] = list.get(i).getKey();
        }
        return ints;
    }
}
